/**
 * 安卓白金英雄坛制作组 <br>
 * 文件名：CreatingScreen.java <br>
 * 创建时间：2013-12-25 下午8:14:41 <br>
 * 所属项目：GmudEX <br>
 * @author 12548 <br>
 */
package lostland.gmud.exv2.ui;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Locale;

import lostland.gmud.exv2.Assets;
import lostland.gmud.exv2.Direction;
import lostland.gmud.exv2.FontSize;
import lostland.gmud.exv2.Game;
import lostland.gmud.exv2.Gmud;
import lostland.gmud.exv2.MainCharTile;
import lostland.gmud.exv2.MapScreen;
import lostland.gmud.exv2.SavingScreen;
import lostland.gmud.exv2.blgframework.CScreen;
import lostland.gmud.exv2.blgframework.impl.BLGGraphics;
import lostland.gmud.exv2.data.Room;
import lostland.gmud.exv2.data.TalentDesc;
import lostland.gmud.exv2.ui.core.MenuScreen;
import lostland.gmud.exv2.ui.core.NewButton;
import lostland.gmud.exv2.ui.core.YesNoScreen;

/**
 * 类名：CreatingScreen <br>
 * 说明：新游戏初始化
 *
 * @author 12548
 */
public class CreatingScreen extends MenuScreen {


	AutoWindow border1, border2;
	int attr[];
	int d = 1;
	String diff[] = new String[]{"简单", "正常", "困难", "骨灰"};

	int sklpoint = 0;
	private ArrayList<String> talentName = new ArrayList<>();
	private ArrayList<Integer> talentID = new ArrayList<>();
	int talentCursor = 0;


	/**
	 * @param instance
	 * @param buttons
	 */
	public CreatingScreen() {
		super(getButtons());

		border1 = new AutoWindow(12 - 3 + 48, 12 - 6, 120, 180, "");
		border2 = new AutoWindow(192 - 3, 12 - 6, 120, 180, "");

		attr = new int[]{
				20,
				20,
				20,
				20
		};

		sklpoint = getGame().data.getSyuume() * 3;

		int syu = getGame().data.getSyuume() >= 5 ? 5 : getGame().data.getSyuume();
		talentID.add(0);
		switch (syu) {
			case 5:
				talentID.add(1008);
				talentID.add(1013);
			case 4:
				talentID.add(1007);
				talentID.add(1012);
			case 3:
				talentID.add(1006);
				talentID.add(1011);
			case 2:
				talentID.add(1005);
				talentID.add(1010);
			case 1:
				talentID.add(1004);
				talentID.add(1009);
			case 0:
				talentID.add(1001);
				talentID.add(1002);
				talentID.add(1003);
		}

		talentName.add("无天赋");

		for (int i = 1; i < talentID.size(); i++) {
			TalentDesc td = Gmud.getTalent_desc().get(talentID.get(i));
			assert td != null;
			talentName.add(td.getName());
		}

	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.MenuScreen#onClick(int)
	 */
	@Override
	protected void onClick(int index) {
		//		if(index < 6 || sklpoint < 0) {
		//			onButtonClick(NewButton.NB_RIGHT);
		//			return;
		//		}
		//
		if (sklpoint < 0) {
			new SimpleNotificationScreen("剩余属性点必须不小于零！").pushToGame();
		} else {
			CScreen scrn = new YesNoScreen("确定属性点分配完毕，开始游戏吗？") {

				@Override
				protected void onYes() {
					Gmud.mc.str = attr[0];
					Gmud.mc.agi = attr[1];
					Gmud.mc.bon = attr[3];
					Gmud.mc.wxg = attr[2];
					Game.getInstance().data.game_difficulty = d;

					Arrays.fill(Gmud.mc.skills, 0);
					Arrays.fill(Gmud.mc.skillsckd, -1);

					//				Gmud.mc.refreshItems();

					if (Gmud.mc.sex == -1) {
						Gmud.mc.sex = Gmud.mc.str < 15 ? 1 : 0;
					}

					Gmud.mc.looking = (int) (30 - Gmud.mc.str + Math.random() * (30 - Gmud.mc.wxg / 2));

					Gmud.mc.food = 100;
					Gmud.mc.drink = 100;

					Gmud.mc.talent = talentID.get(talentCursor);
					Gmud.mc.give(42);

//					Gmud.ms = new MapScreen(game);

					MapScreen.getInstance().jumpTo(1, 9, 7, Direction.DOWN);

					//				Gmud.ms.setMap(Gmud.maps[1]);
					getGame().data.addfp(1, MainCharTile.getInstance().X, MainCharTile.getInstance().Y, MainCharTile.getInstance().currentDirection);


					new Room().after_process();
					Gmud.mc.refresh();
					new SavingScreen().save();


					getGame().popToRootScreen();
				}

				@Override
				protected void onNo() {
					getGame().popScreen();
				}
			};
			scrn.pushToGame();
		}


	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.MenuScreen#onCancel()
	 */
	@Override
	public void onCancel() {
		// DO NOTHING
	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.ButtonControlledScreen#show()
	 */
	@Override
	protected void show() {
		BLGGraphics g = (BLGGraphics) getGame().getGraphics();
		g.clear(Gmud.getBgColor());
		border1.draw();
		border2.draw();


		for (int i = 0; i < 7; i++) {
			buttons[i].draw();
			if (i < 5) {
				g.drawPixmap(Assets.left, 72 - 3 + 48 - 3, 24 + 21 * i - 6 - 6);
				g.drawPixmap(Assets.right, 108 - 3 + 48 + 3, 24 + 21 * i - 6 - 6);
			}
			if (i < 4) {
				g.drawText(attr[i] + "", 96 - 6 - 3 + 48, 24 + 21 * i - 6 - 6, FontSize.FT_12PX);
			}
		}

		g.drawText(diff[d], 84 - 3 + 48, 24 + 21 * 4 - 6 - 6, FontSize.FT_12PX);
		g.drawText(talentName.get(talentCursor), 84 - 3 + 48 - 12, 24 + 21 * 5 - 6 - 6, FontSize.FT_12PX);

		String s = String.format(Locale.getDefault(), "剩余属性点：  %d%n%n    ", sklpoint);

		final String attr_desc[] = new String[]{
				"膂力代表人物肌肉力量的强度，它影响你的攻击伤害、命中率、招架能力和饭量大小。" +
						"不论是徒手搏击还是持械相斗，膂力越大，当然敌人受的伤便越重。",
				"敏捷影响一个人的攻防及躲避的能力。敏捷越高，便越容易避开对手的攻击，" +
						"同时敏捷高的人可以更轻易地攻击到对方，从而快速解决战斗。",
				"悟性简单地说就是你的学习领会能力，包括读书、理解师傅的教诲等。" +
						"悟性高的人花同样多的潜能能学到比悟性低的人更多的东西，读书也来得更快。",
				"根骨主要影响气力的恢复速度。根骨越好的人恢复得越快，短时休息后就又能投入战斗。" +
						"根骨好的人在战斗中具有敏感的判断力，当招式加身时会自然的顺势闪避，以免伤及要害。",
				"难度越高，战斗中触发额外效果（连击、暴击、反击）的几率越高，" +
						"NPC使用绝招、吸气的概率越高，NPC战斗时携带的药物越多。",
				"天赋是赋予你的特殊能力。\n\n",
				"准备好了吗？点击开始你的英雄坛之旅。",
		};

		s += attr_desc[cursor];

		if (cursor == 5 && talentCursor > 0) {
			s += Gmud.getTalent_desc().get(talentID.get(talentCursor)).getAll();
		}

		g.drawText(s, 192 - 3 + 6, 24 - 12, FontSize.FT_12PX, 108);

	}

	private static AutoButton[] getButtons() {
		AutoButton[] t;
		t = new AutoButton[7];
		String s[] = new String[]{"膂力", "敏捷", "悟性", "根骨", "难度", "天赋", "开始游戏"};
		for (int i = 0; i < t.length; i++) {
			t[i] = new AutoButton(36 - 3 - 12 + 48, 24 + 21 * i - 6 - 6, s[i]);
		}

		return t;
	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.MenuScreen#onButtonClick(lostland.gmud.exv2.ui.core.NewButton)
	 */
	@Override
	public void onButtonClick(NewButton b) {
		if (b == NewButton.NB_LEFT) {
			if (cursor < 4) {
				if (attr[cursor] > 10) {
					attr[cursor]--;
					sklpoint++;
				}
			} else if (cursor == 4) {
				d--;
				if (d < 0) {
					d = 3;
				}
			} else if (cursor == 5) {
				talentCursor--;
				if (talentCursor < 0) {
					talentCursor = talentName.size() - 1;
				}
			}
		} else if (b == NewButton.NB_RIGHT) {
			if (cursor < 4) {
				if (attr[cursor] < 30) {
					attr[cursor]++;
					sklpoint--;
				}
			} else if (cursor == 4) {
				d++;
				if (d > 3) {
					d = 0;
				}
			} else if (cursor == 5) {
				talentCursor++;
				if (talentCursor >= talentName.size()) {
					talentCursor = 0;
				}
			}
		} else {
			super.onButtonClick(b);
		}
	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.DialogScreen#isStable()
	 */
	@Override
	public boolean isStable() {
		return false;
	}
}
